<template>
  <div v-if="getShow" class="modern-qrcode-login-wrapper">
    <div class="modern-qrcode-login-container">
      <div class="form-header">
        <h2>{{ t('sys.login.qrCodeLogin') }}</h2>
        <p>{{ t('sys.login.qrCodeLoginDesc') }}</p>
      </div>

      <div class="qrcode-content">
        <div class="qrcode-wrapper">
          <div class="qrcode-container" :class="{ scanning: state === '1', success: state === '2' }">
            <QrCode :value="qrCodeUrl" :width="280" class="qrcode-image" />
            
            <!-- 扫描状态遮罩 -->
            <div v-if="state === '1'" class="scan-overlay">
              <div class="scan-line"></div>
              <div class="scan-text">{{ t('sys.login.scanning') }}</div>
            </div>
            
            <!-- 成功状态遮罩 -->
            <div v-if="state === '2'" class="success-overlay">
              <div class="success-icon">
                <CheckCircleFilled />
              </div>
              <div class="success-text">{{ t('sys.login.scanSuccess') }}</div>
            </div>
            
            <!-- 过期状态 -->
            <div v-if="state === '-2'" class="expired-overlay">
              <div class="expired-icon">
                <ExclamationCircleFilled />
              </div>
              <div class="expired-text">{{ t('sys.login.qrCodeExpired') }}</div>
              <button class="refresh-btn" @click="loadQrCode">
                <ReloadOutlined />
                {{ t('sys.login.refreshQrCode') }}
              </button>
            </div>
          </div>
        </div>

        <div class="qrcode-status">
          <div class="status-text" :class="getStatusClass()">
            {{ scanContent }}
          </div>
          <div class="status-tips">
            <div class="tip-item">
              <MobileOutlined />
              <span>{{ t('sys.login.qrCodeTip1') }}</span>
            </div>
            <div class="tip-item">
              <ScanOutlined />
              <span>{{ t('sys.login.qrCodeTip2') }}</span>
            </div>
          </div>
        </div>

        <div class="form-footer">
          <button type="button" class="back-btn" @click="handleBackLogin">
            {{ t('sys.login.backSignIn') }}
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { computed, unref, ref, watch, onBeforeUnmount } from 'vue';
  import { CheckCircleFilled, ExclamationCircleFilled, ReloadOutlined, MobileOutlined, ScanOutlined } from '@ant-design/icons-vue';
  import { QrCode } from '/@/components/Qrcode/index';
  import { useUserStore } from '/@/store/modules/user';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useLoginState, LoginStateEnum } from './useLogin';
  import { getLoginQrcode, getQrcodeToken } from '/@/api/sys/user';

  const qrCodeUrl = ref('');
  let timer: IntervalHandle;
  const { t } = useI18n();
  const userStore = useUserStore();
  const { handleBackLogin, getLoginState } = useLoginState();
  const state = ref('0');
  
  const getShow = computed(() => unref(getLoginState) === LoginStateEnum.QR_CODE);
  
  const scanContent = computed(() => {
    switch (unref(state)) {
      case '0':
        return t('sys.login.scanSign');
      case '1':
        return t('sys.login.scanning');
      case '2':
        return t('sys.login.scanSuccess');
      case '-2':
        return t('sys.login.qrCodeExpired');
      default:
        return t('sys.login.scanSign');
    }
  });

  const getStatusClass = () => {
    switch (unref(state)) {
      case '0':
        return 'status-waiting';
      case '1':
        return 'status-scanning';
      case '2':
        return 'status-success';
      case '-2':
        return 'status-expired';
      default:
        return 'status-waiting';
    }
  };

  // 加载二维码信息
  function loadQrCode() {
    state.value = '0';
    getLoginQrcode().then((res) => {
      qrCodeUrl.value = res.qrcodeId;
      if (res.qrcodeId) {
        openTimer(res.qrcodeId);
      }
    }).catch(() => {
      state.value = '-2';
    });
  }

  // 监控扫码状态
  function watchQrcodeToken(qrcodeId) {
    getQrcodeToken({ qrcodeId: qrcodeId }).then((res) => {
      let token = res.token;
      if (token == '-2') {
        // 二维码过期重新获取
        state.value = '-2';
        loadQrCode();
        clearInterval(timer);
      }
      // 扫码成功
      if (res.success) {
        state.value = '2';
        clearInterval(timer);
        setTimeout(() => {
          userStore.qrCodeLogin(token);
        }, 500);
      }
    }).catch(() => {
      state.value = '-2';
    });
  }

  /** 开启定时器 */
  function openTimer(qrcodeId) {
    watchQrcodeToken(qrcodeId);
    closeTimer();
    timer = setInterval(() => {
      watchQrcodeToken(qrcodeId);
    }, 1500);
  }

  /** 关闭定时器 */
  function closeTimer() {
    if (timer) clearInterval(timer);
  }

  watch(getShow, (v) => {
    if (v) {
      loadQrCode();
    } else {
      closeTimer();
    }
  });

  onBeforeUnmount(() => {
    closeTimer();
  });
</script>

<style scoped lang="less">
  .modern-qrcode-login-wrapper {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
  }

  .modern-qrcode-login-container {
    width: 100%;
    max-width: 480px;
    border-radius: 24px;
    padding: 48px;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    position: relative;
    z-index: 1;
    animation: fadeInUp 0.6s ease-out;

    @keyframes fadeInUp {
      from {
        opacity: 0;
        transform: translateY(30px);
      }
      to {
        opacity: 1;
        transform: translateY(0);
      }
    }
  }

  .form-header {
    text-align: center;
    margin-bottom: 40px;

    h2 {
      color: #2c3e50;
      font-size: 32px;
      margin-bottom: 12px;
      font-weight: 700;
      background: linear-gradient(135deg, #0089ff, #00b4ff);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      background-clip: text;
    }

    p {
      color: #7f8c8d;
      font-size: 16px;
      margin: 0;
    }
  }

  .qrcode-content {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    width: 100%;
  }

  .qrcode-wrapper {
    margin-bottom: 32px;
    width: 100%;
    display: flex;
    justify-content: center;
  }

  .qrcode-container {
    position: relative;
    border-radius: 20px;
    padding: 20px;
    background: rgba(255, 255, 255, 0.9);
    box-shadow: 0 8px 32px rgba(0, 137, 255, 0.1);
    border: 2px solid rgba(0, 137, 255, 0.1);
    transition: all 0.3s ease;

    &:hover {
      box-shadow: 0 12px 40px rgba(0, 137, 255, 0.15);
      border-color: rgba(0, 137, 255, 0.2);
    }

    &.scanning {
      border-color: #f39c12;
      box-shadow: 0 8px 32px rgba(243, 156, 18, 0.2);
    }

    &.success {
      border-color: #27ae60;
      box-shadow: 0 8px 32px rgba(39, 174, 96, 0.2);
    }
  }

  .qrcode-image {
    border-radius: 12px;
    overflow: hidden;
  }

  .scan-overlay,
  .success-overlay,
  .expired-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    animation: fadeIn 0.3s ease;
  }

  .scan-overlay {
    .scan-line {
      width: 100%;
      height: 2px;
      background: linear-gradient(90deg, transparent, #f39c12, transparent);
      animation: scanMove 2s linear infinite;
      margin-bottom: 16px;
    }

    .scan-text {
      color: #f39c12;
      font-size: 16px;
      font-weight: 600;
    }
  }

  .success-overlay {
    .success-icon {
      font-size: 48px;
      color: #27ae60;
      margin-bottom: 16px;
      animation: successBounce 0.6s ease;
    }

    .success-text {
      color: #27ae60;
      font-size: 16px;
      font-weight: 600;
    }
  }

  .expired-overlay {
    .expired-icon {
      font-size: 48px;
      color: #e74c3c;
      margin-bottom: 16px;
    }

    .expired-text {
      color: #e74c3c;
      font-size: 16px;
      font-weight: 600;
      margin-bottom: 16px;
    }

    .refresh-btn {
      background: linear-gradient(135deg, #0089ff, #00b4ff);
      color: white;
      border: none;
      border-radius: 12px;
      padding: 12px 24px;
      font-size: 14px;
      font-weight: 500;
      cursor: pointer;
      transition: all 0.3s ease;
      display: flex;
      align-items: center;
      gap: 8px;

      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(0, 137, 255, 0.3);
      }
    }
  }

  .qrcode-status {
    text-align: center;
    margin-bottom: 32px;
    width: 100%;

    .status-text {
      font-size: 18px;
      font-weight: 600;
      margin-bottom: 16px;
      padding: 12px 24px;
      border-radius: 12px;
      transition: all 0.3s ease;

      &.status-waiting {
        color: #7f8c8d;
        background: rgba(127, 140, 141, 0.1);
      }

      &.status-scanning {
        color: #f39c12;
        background: rgba(243, 156, 18, 0.1);
        animation: pulse 2s infinite;
      }

      &.status-success {
        color: #27ae60;
        background: rgba(39, 174, 96, 0.1);
      }

      &.status-expired {
        color: #e74c3c;
        background: rgba(231, 76, 60, 0.1);
      }
    }

    .status-tips {
      display: flex;
      flex-direction: column;
      gap: 12px;

      .tip-item {
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
        color: #7f8c8d;
        font-size: 14px;

        .anticon {
          font-size: 16px;
        }
      }
    }
  }

  .form-footer {
    text-align: center;
    width: 100%;

    .back-btn {
      width: 100%;
      max-width: 100%;
      background: transparent;
      border: 2px solid #e1e8ed;
      color: #7f8c8d;
      padding: 16px 24px;
      border-radius: 16px;
      font-size: 16px;
      font-weight: 500;
      cursor: pointer;
      transition: all 0.3s ease;
      display: flex;
      align-items: center;
      justify-content: center;
      box-sizing: border-box;

      &:hover {
        border-color: #0089ff;
        color: #0089ff;
        background: rgba(0, 137, 255, 0.05);
        transform: translateY(-1px);
      }
    }
  }

  @keyframes scanMove {
    0% {
      transform: translateY(0);
    }
    50% {
      transform: translateY(280px);
    }
    100% {
      transform: translateY(0);
    }
  }

  @keyframes successBounce {
    0% {
      transform: scale(0);
    }
    50% {
      transform: scale(1.2);
    }
    100% {
      transform: scale(1);
    }
  }

  @keyframes pulse {
    0%, 100% {
      opacity: 1;
    }
    50% {
      opacity: 0.7;
    }
  }

  @keyframes fadeIn {
    from {
      opacity: 0;
    }
    to {
      opacity: 1;
    }
  }

  // 响应式设计
  @media (max-width: 768px) {
    .modern-qrcode-login-container {
      max-width: 400px;
      padding: 32px 24px;
    }

    .form-header h2 {
      font-size: 28px;
    }

    .qrcode-container {
      padding: 16px;
    }

    .form-footer .back-btn {
      padding: 14px 20px;
      font-size: 14px;
    }
  }

  @media (max-width: 480px) {
    .modern-qrcode-login-wrapper {
      padding: 16px;
    }

    .modern-qrcode-login-container {
      padding: 24px 20px;
    }

    .form-header h2 {
      font-size: 24px;
    }

    .form-header p {
      font-size: 14px;
    }

    .qrcode-container {
      padding: 12px;
    }

    .form-footer .back-btn {
      padding: 12px 16px;
      font-size: 14px;
    }
  }

  // 深色模式支持
  @media (prefers-color-scheme: dark) {
    .modern-qrcode-login-container {
      background: rgba(44, 62, 80, 0.95);
      border-color: rgba(255, 255, 255, 0.1);
    }

    .form-header h2 {
      color: #ecf0f1;
    }

    .form-header p {
      color: #bdc3c7;
    }

    .qrcode-container {
      background: rgba(52, 73, 94, 0.9);
      border-color: rgba(0, 137, 255, 0.2);
    }

    .scan-overlay,
    .success-overlay,
    .expired-overlay {
      background: rgba(52, 73, 94, 0.95);
    }

    .qrcode-status .status-tips .tip-item {
      color: #bdc3c7;
    }

    .form-footer .back-btn {
      border-color: #34495e;
      color: #bdc3c7;

      &:hover {
        border-color: #0089ff;
        color: #0089ff;
        background: rgba(0, 137, 255, 0.1);
      }
    }
  }
</style>
